package com.fengdu.controller;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fengdu.common.Sid;
import com.fengdu.entity.Orders;
import com.fengdu.entity.Product;
import com.fengdu.entity.weixin.PayResult;
import com.fengdu.entity.weixin.PreOrderResult;
import com.fengdu.service.OrdersService;
import com.fengdu.service.ProductService;
import com.fengdu.service.WxOrderService;

/**
 * 
 * <p>
 * Title: WxpayController.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2018
 * </p>
 * 
 * @author tiankong
 * @email 2366207000@qq.com
 * @date 2018年6月26日
 * @version 1.0
 */
@Controller
@RequestMapping("/wxpay")
public class WxpayController {

	@Autowired
	private ProductService productService;

	@Autowired
	private OrdersService orderService;

	@Autowired
	private WxOrderService wxOrderService;

	@Autowired
	private Sid sid;

	@RequestMapping(value = "/index")
	public Object products(Map<String, Object> map) throws Exception {
		List<Product> pList = productService.getProducts();
		map.put("pList", pList);
		return "index";
	}

	// TODO 实际情况需要自己业务订单的状态，此处仅仅用于测试
	public static boolean isOrderPaid = false;

	/**
	 * 
	 * @Description: 生成预付单
	 * @param amount
	 * @param title
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/createPreOrder")
	public Object createPreOrder(String orderId, HttpServletRequest request, HttpServletResponse response,
			Map<String, Object> map) throws Exception {

		Orders order = orderService.getOrderById(orderId);

		Product p = productService.getProductById(order.getProductId());

		// ModelAndView mv = new ModelAndView("goPay");
		// mv.addObject("order", order);
		// mv.addObject("p", p);

		// 商品描述
		String body = p.getName();
		// 商户订单号
		String out_trade_no = sid.nextShort();
		// 订单总金额，单位为分
		String total_fee = "2";
		// 统一下单
		PreOrderResult preOrderResult = wxOrderService.placeOrder(body, out_trade_no, total_fee);

		map.put("qrCodeUrl", preOrderResult.getCode_url());

		return "template/pay/pay_qr_code";
	}

	/**
	 * 
	 * @Description: 扫码完毕后每隔3秒检查是否支付成功
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 * 
	 */
	@RequestMapping(value = "/wxPayIsSuccess")
	@ResponseBody
	public boolean wxPayIsSuccess(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO 查看订单是否支付成功，成功返回true，失败返回false
		return isOrderPaid;
	}

	/**
	 * 
	 * @Description: 微信支付异步通知 1. 支付成功修改订单状态，标记为成功 2. 生成支付流水
	 * @param amount
	 * @param title
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/notify")
	public void notify(HttpServletRequest request, HttpServletResponse response) throws Exception {

		PayResult payResult = wxOrderService.getWxPayResult(request);

		boolean isPaid = payResult.getReturn_code().equals("SUCCESS") ? true : false;

		// 查询该笔订单在微信那边是否成功支付
		// 支付成功，商户处理后同步返回给微信参数
		PrintWriter writer = response.getWriter();
		if (isPaid) {
			System.out.println("================================= 支付成功 =================================");

			// ====================== 操作商户自己的业务，比如修改订单状态，生成支付流水等 start
			// ==========================
			// TODO
			this.isOrderPaid = true;
			// ============================================ 业务结束， end
			// ==================================

			// 通知微信已经收到消息，不要再给我发消息了，否则微信会8连击调用本接口
			String noticeStr = setXML("SUCCESS", "");
			writer.write(noticeStr);
			writer.flush();

		} else {
			System.out.println("================================= 支付失败 =================================");

			// 支付失败
			String noticeStr = setXML("FAIL", "");
			writer.write(noticeStr);
			writer.flush();
		}

	}

	/**
	 * 
	 * @Description: 支付成功
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/paySuccess")
	public String paySuccess() throws Exception {

		return "template/pay/pay_success";
	}

	public static String setXML(String return_code, String return_msg) {
		return "<xml><return_code><![CDATA[" + return_code + "]]></return_code><return_msg><![CDATA[" + return_msg
				+ "]]></return_msg></xml>";
	}
}
